package com.aurora.source.center.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.base.BaseRecyclerAdapter;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.center.adapter.OprationRecoredAdapter;
import com.aurora.source.center.bean.OprationRecoredBean;
import com.aurora.source.center.bean.PersonFlowBean;
import com.aurora.source.center.presenter.OperationRecoredPresenter;
import com.aurora.source.center.view.IOperationRecoredView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: ww
 * @description:主流程操作记录
 * @date: 2018/9/7
 */
public class OperationRecoredActivity extends BaseWhiteToolbarActivity<OperationRecoredPresenter> implements IOperationRecoredView {


    @BindView(R.id.rv_recored)
    RecyclerView rvRecored;
    @BindView(R.id.tv_flow_select)
    TextView tvFlowSelect;
    List<PersonFlowBean> flowList;//流程权限
    List<PersonFlowBean> haveFlowList;//拥有的流程权限

    PersonFlowBean currentFlow;//当前流程
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private OprationRecoredAdapter oprationRecoredAdapter;
    private String selectProcessId;
    int pageNo = 0;
    int pageSize = 15;
    @Override
    public OperationRecoredPresenter attach() {
        return new OperationRecoredPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_operation_recored;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        haveFlowList = new ArrayList<>();
        flowList = (List<PersonFlowBean>) bundle.getSerializable("flowList");
        if(flowList!=null&&flowList.size()>0){
            for(int i=0;i<flowList.size();i++){
                if(flowList.get(i).isHavaPermision()){
                    haveFlowList.add(flowList.get(i));
                }
            }
        }
        currentFlow = (PersonFlowBean) bundle.getSerializable("currentFlow");
        selectProcessId = currentFlow.getFlowId();
        tvFlowSelect.setText(currentFlow.getFlowName());
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        refresh.setEnableAutoLoadMore(false);
        refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshUI();
            }
        });
        oprationRecoredAdapter = new OprationRecoredAdapter();
        //新增记录
        oprationRecoredAdapter.setOnNewRecoredClickListener(new OprationRecoredAdapter.OnNewRecoredClickListener() {
            @Override
            public void onNewClick(int position, OprationRecoredBean data) {
                Bundle bundle = new Bundle();
                bundle.putString("processId", selectProcessId);
                bundle.putString("parentId", data.getParentId());
                bundle.putString("batchNo", data.getBatchNo());
                gotoActivity(AddFlowRecoredActivity.class, false, bundle);
            }
        });

        //查看记录
        oprationRecoredAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<OprationRecoredBean>() {
            @Override
            public void onClick(int position, OprationRecoredBean data) {
                Bundle bundle = new Bundle();
                bundle.putString("processId", selectProcessId);
                bundle.putString("parentId", data.getParentId());
                bundle.putString("batchNo", data.getBatchNo());
                bundle.putString("parentBatchName", data.getParentBatchName());
                bundle.putString("parentTitle", data.getImplementedTitle());
                bundle.putString("isWinding", data.getIsWinding());
                bundle.putString("remark",data.getRemark());
                gotoActivity(FlowRecoredDetailActivity.class, false, bundle);
            }
        });
        simpleToolbar.setMainTitle("操作记录");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
        rvRecored.setLayoutManager(new LinearLayoutManager(this));
        rvRecored.setAdapter(oprationRecoredAdapter);
    }

    @Override
    public void doBusiness() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshUI();
    }

    private void refreshUI() {
        pageNo = 0;
        Map<String, String> params = new HashMap<>();
        params.put("userId", userId);
        params.put("processId", selectProcessId);
        params.put("isMain", "Y");
        params.put("pageNo",pageNo+"");
        params.put("pageSize",pageSize+"");
        basePresenter.queryRecordingList(params);
    }

    private void loadMore() {
        pageNo++;
        Map<String, String> params = new HashMap<>();
        params.put("userId", userId);
        params.put("processId", selectProcessId);
        params.put("isMain", "Y");
        params.put("pageNo",pageNo+"");
        params.put("pageSize",pageSize+"");
        basePresenter.queryRecordingList(params);
    }


    @OnClick(R.id.tv_flow_select)
    public void onViewClicked() {
        if (haveFlowList == null) {
            return;
        }
        String strings[] = new String[haveFlowList.size()];
        for (int i = 0, j = haveFlowList.size(); i < j; i++) {
            strings[i] = haveFlowList.get(i).getFlowName();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setSingleChoiceItems(strings, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                selectProcessId = haveFlowList.get(which).getFlowId();
                tvFlowSelect.setText(haveFlowList.get(which).getFlowName());
                dialog.dismiss();
                refreshUI();
            }
        });
        builder.create().show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onGetRecoredFail(String msg) {
        showToast(msg);
        refresh.finishRefresh();
        refresh.finishLoadMore();
    }

    @Override
    public void onGetRecoredSuccess(List<OprationRecoredBean> recordingList) {
        if(pageNo==0){
            oprationRecoredAdapter.setDataList(recordingList);
            refresh.finishRefresh();
        }else {
            oprationRecoredAdapter.addItems(recordingList);
            refresh.finishLoadMore();
        }

    }

    @Override
    public void onFinishFlowSuccess(String msg) {

    }

    @Override
    public void onFinishFlowFail(String msg) {

    }
}
